<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 发送短信控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\SMSService;
use think\Request;

class SMSController extends VerifyController
{
    private $smsService;

    public function __construct()
    {
        parent::__construct();
        $this->smsService = new SMSService();
    }

    /**
     * 发送注册短信
     * @param Request $request
     * @return \think\Response
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function sendRegisterSMS(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->smsService->sendRegisterSMS($param['phone']);
            return json(success_response('SMS sent successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送语音消息
     * @param Request $request
     * @return \think\Response|void
     */
    public function sendVoiceSMS(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->smsService->sendVoiceSMS($param['phone'], $param['template_id'], $param['template_content_data']);
            return json(success_response('Voice SMS sent successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送登陆短信
     * @param Request $request
     * @return \think\Response
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function sendLoginSMS(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->smsService->sendLoginSMS($param['phone']);
            return json(success_response('SMS sent successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送找回密码短信
     * @param Request $request
     * @return \think\Response
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     */
    public function sendForgotSMS(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->smsService->sendForgotSMS($param['phone']);
            return json(success_response('SMS sent successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}